//
//  MyCollectionViewC.m
//  WHSport
//
//  Created by ccjoy-jimneylee on 15/3/31.
//  Copyright (c) 2015年 jimneylee. All rights reserved.
//

#import "MyCollectionViewC.h"
#import "PlaceEntity.h"
#import <UIImageView+AFNetworking.h>
#import "Util.h"
#import "PlaceDetailViewC.h"
#import <MBProgressHUD.h>

@interface CollectionCell : UITableViewCell
@property(nonatomic, weak) IBOutlet UIImageView *placeImg;
@property(nonatomic, weak) IBOutlet UIImageView *levelImg;
@property(nonatomic, weak) IBOutlet UILabel *nameLab;
@property(nonatomic, weak) IBOutlet UILabel *evaLab;
@property(nonatomic, weak) IBOutlet UILabel *nowLab;
@property(nonatomic, weak) IBOutlet UILabel *oriLab;
@property(nonatomic, weak) IBOutlet UILabel *placeLab;
@property(nonatomic, weak) IBOutlet UILabel *disLab;
@property(nonatomic, weak) IBOutlet UIButton *deleteBtn;
@end
@implementation CollectionCell
@end

@interface MyCollectionViewC (){
    NSMutableArray *dataArray;
}

@end

@implementation MyCollectionViewC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title =@"我的收藏";
    dataArray = [[NSMutableArray alloc] init];
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData{
    
    NSString *filename= [NSString stringWithFormat:
                         @"%@/Documents/Place.plist",NSHomeDirectory()];
    NSArray *typeA  = [NSArray arrayWithContentsOfFile:filename];
    for (NSDictionary *dic in typeA) {
        PlaceEntity *placeEty = [[PlaceEntity alloc]
                                 initWithAttributes:dic];
        [dataArray addObject:placeEty];
    }
    if (dataArray.count == 0) {
        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        HUD.labelText = @"没有数据";
        [HUD hide:YES afterDelay:1.5f];
    }
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90.f;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CollectionID"
                             forIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    PlaceEntity *placeEntity = dataArray[indexPath.row];
    
    cell.nameLab.text = placeEntity.name;
    
    NSString *eva = [NSString stringWithFormat:@"%@评论",
                     placeEntity.evaluationCount];
    cell.evaLab.text = eva;
    NSString *now = [NSString stringWithFormat:@"%@",
                     placeEntity.nowPrice];
    cell.nowLab.text = now;
    NSString *ori = [NSString stringWithFormat:@"%@元",
                     placeEntity.originalPrice];
    cell.oriLab.text = ori;
    cell.placeLab.text = placeEntity.address;
    NSString *dis = [NSString stringWithFormat:@"%@km",
                     placeEntity.distance];
    cell.disLab.text = dis;
    NSString *level = [NSString stringWithFormat:@"%@",
                       placeEntity.level];
    NSString *levelImg = [Util getstrWithstarNum:level];
    cell.levelImg.image = [UIImage imageNamed:levelImg];
    NSString *img = [NSString stringWithFormat:@"http://218.93.12.178:8080/hhw-api/%@",
                     placeEntity.imgPath];
    NSURL *url = [NSURL URLWithString:img];
    [cell.placeImg setImageWithURL:url];
    cell.deleteBtn.tag = indexPath.row;
    [cell.deleteBtn addTarget:self action:@selector(deleteBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
    
}

- (void)deleteBtnAction:(id)sender{
    UIButton *btn=(UIButton *)sender;
    NSString *fileName= [NSString stringWithFormat:
                         @"%@/Documents/Place.plist",NSHomeDirectory()];
    NSMutableArray *allArray = [NSMutableArray
                                arrayWithContentsOfFile:fileName];
    [allArray removeObjectAtIndex:btn.tag];    
    [allArray writeToFile:fileName atomically:YES];
    [dataArray removeObjectAtIndex:btn.tag];
    [self.tableView reloadData];
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    UIStoryboard *main = [UIStoryboard storyboardWithName:@"Main"
                                                   bundle:nil];
    PlaceDetailViewC *placevc = [main instantiateViewControllerWithIdentifier:@"PlaceDetailViewC"];
    placevc.currentPlace = dataArray[indexPath.row];
    [self.navigationController pushViewController:placevc animated:YES];
    
}




/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
